﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PMCSerialTransfer.Packets
{
    class RecvFileListPacket : APacket
    {
         public override bool Packet(byte[] Data)
        {
            return Process(Data);
        }

         public override bool EndProcess(bool Ok)
         {
             Encoding utf8 = Encoding.UTF8;
             bool err= false;
             
             SerialSettings settings = new SerialSettings();
             settings.GetSettings();

             switch (Globals.DataPacket[0])
             {
                 case (byte)Globals.ACKType.E_ACK:
                     if (Globals.FileIndex == -1)
                     {
                         Globals.Files = Directory.GetFiles(settings.DefaultFolder);
                         Globals.FileIndex++;
                     }
                     else
                         Globals.FileIndex++;

                     err = false;
                     break;
                 case (byte)Globals.ACKType.E_NACK:
                     err = true;
                     break;
                 case (byte)Globals.ACKType.E_ERR:
                     Globals.Message = "10 Errors occured, aborting\n";
                     return Ok;

             }
                
             if (Globals.ErrorCount < 10)
             {
                 FileInfo fi = new FileInfo(Globals.Files[Globals.FileIndex]);
                 if (err)
                 {
                     Globals.Message = "CRC Error.. Retrying\n";
                     Globals.DataPacket = new byte[1];
                     Globals.DataPacket[0] = 2;
                 }
                 else
                 {
                     Globals.Message = fi.Name + " Bytes: " + fi.Length.ToString() + "\n";
                     Globals.DataPacket = new byte[fi.Name.Length + 5];
                     Globals.DataPacket[0] = (byte)CommFactory.Command.RecvList;
                     if (Globals.FileIndex == Globals.Files.Length - 1)
                     {
                         Globals.DataPacket[0] = 0;
                         Globals.FileIndex = -1;
                     }
                     else
                     {
                         Globals.DataPacket[0] = 1;
                     }

                     byte[] size = BitConverter.GetBytes(fi.Length);
                     for (int i = 0; i < 4; i++)
                     {
                         if (BitConverter.IsLittleEndian)
                         {
                             Globals.DataPacket[i + 1] = size[3 - i];
                         }
                         else
                         {
                             Globals.DataPacket[i + 1] = size[i];
                         }
                     }
                     byte[] filebyte = utf8.GetBytes(fi.Name);
                     for (int i = 0; i < filebyte.Length; i++)
                     {
                         Globals.DataPacket[i + 5] = filebyte[i];
                     }
                 }
                 Globals.Create((byte)CommFactory.Command.RecvList, Globals.DataPacket);
             }
             return Ok;
         }
    }
}
